/*
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * as published by the Free Software Foundation; either version 2
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, 
 * MA  02110-1301, USA.
 *
 * http://www.gnu.org/copyleft/gpl.html
 */
package parser.serverData.recipe;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.util.Collection;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.PropertyException;

import parser.clientData.clientStrings.ClientString;
import parser.clientData.recipe.ClientCombineRecipe;
import parser.util.DataManager;

/**
 * @author Mr. Poke
 *
 */
public class RecipeSave
{

	public static void save()
	{
		ObjectFactory objFactory = new ObjectFactory();
		RecipeTemplates collection = (RecipeTemplates) objFactory.createRecipeTemplates();
		List<RecipeTemplate> templateList = collection.getRecipeTemplate();

		Collection<ClientCombineRecipe> recipes = DataManager.getInstance().getRecipeList();
		for (ClientCombineRecipe cRecipe: recipes)
		{
			RecipeTemplate recipe = new RecipeTemplate();
			recipe.setId(cRecipe.getId());
			if (cRecipe.getRequireDp() != null && cRecipe.getRequireDp() > 0)
				recipe.setDp(cRecipe.getRequireDp());
			ClientString name = DataManager.getInstance().getStringNameMap().get(cRecipe.getDesc().toLowerCase());
			recipe.setNameid(name.getId()*2+1);
			if (cRecipe.getAutoLearn() == 1)
				recipe.setAutolearn(true);
			recipe.setQuantity(cRecipe.getProductQuantity());
			recipe.setSkillid(DataManager.getInstance().getClientSkillsnameMap().get(cRecipe.getCombineskill().toUpperCase()).getId());
			recipe.setSkillpoint(cRecipe.getRequiredSkillpoint());
			if (cRecipe.getQualificationRace().equalsIgnoreCase("pc_light"))
				recipe.setRace(Race.ELYOS);
			else if (cRecipe.getQualificationRace().equalsIgnoreCase("pc_dark"))
				recipe.setRace(Race.ASMODIANS);
			else
				recipe.setRace(Race.PC_ALL);
			Component comp1 = new Component();
			comp1.setQuantity(cRecipe.getCompo1Quantity());
			comp1.setItemid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getComponent1().toLowerCase()));
			recipe.getComponent().add(comp1);
			if (cRecipe.getComponent2() != null)
			{
				Component comp = new Component();
				comp.setQuantity(cRecipe.getCompo2Quantity());
				comp.setItemid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getComponent2().toLowerCase()));
				recipe.getComponent().add(comp);
			}
			if (cRecipe.getComponent3() != null)
			{
				Component comp = new Component();
				comp.setQuantity(cRecipe.getCompo3Quantity());
				comp.setItemid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getComponent3().toLowerCase()));
				recipe.getComponent().add(comp);
			}
			if (cRecipe.getComponent4() != null)
			{
				Component comp = new Component();
				comp.setQuantity(cRecipe.getCompo4Quantity());
				comp.setItemid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getComponent4().toLowerCase()));
				recipe.getComponent().add(comp);
			}
			if (cRecipe.getComponent5() != null)
			{
				Component comp = new Component();
				comp.setQuantity(cRecipe.getCompo5Quantity());
				comp.setItemid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getComponent5().toLowerCase()));
				recipe.getComponent().add(comp);
			}
			if (cRecipe.getComponent6() != null)
			{
				Component comp = new Component();
				comp.setQuantity(cRecipe.getCompo6Quantity());
				comp.setItemid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getComponent6().toLowerCase()));
				recipe.getComponent().add(comp);
			}
			if (cRecipe.getComponent7() != null)
			{
				Component comp = new Component();
				comp.setQuantity(cRecipe.getCompo7Quantity());
				comp.setItemid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getComponent7().toLowerCase()));
				recipe.getComponent().add(comp);
			}
			if (cRecipe.getCombo1Product() != null)
			{
				ComboProduct combo = new ComboProduct();
				combo.setItemid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getCombo1Product().toLowerCase()));
				recipe.getComboproduct().add(combo);
			}
			if (cRecipe.getCombo2Product() != null)
			{
				ComboProduct combo = new ComboProduct();
				combo.setItemid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getCombo2Product().toLowerCase()));
				recipe.getComboproduct().add(combo);
			}
			if (cRecipe.getCombo3Product() != null)
			{
				ComboProduct combo = new ComboProduct();
				combo.setItemid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getCombo3Product().toLowerCase()));
				recipe.getComboproduct().add(combo);
			}
			if (cRecipe.getCombo4Product() != null)
			{
				ComboProduct combo = new ComboProduct();
				combo.setItemid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getCombo4Product().toLowerCase()));
				recipe.getComboproduct().add(combo);
			}
			
			recipe.setProductid(DataManager.getInstance().getItemNameIdMap().get(cRecipe.getProduct().toLowerCase()));
			
			if (cRecipe.getMaxProductionCount() != null && cRecipe.getMaxProductionCount() > 0)
				recipe.setMaxProductionCount(cRecipe.getMaxProductionCount());
			
			if (cRecipe.getCraftDelayId() > 0)
				recipe.setCraftDelayId(cRecipe.getCraftDelayId());
			if (cRecipe.getCraftDelayTime() > 0)
				recipe.setCraftDelayTime(cRecipe.getCraftDelayTime());
			templateList.add(recipe);
		}
		
		try
		{
			JAXBContext jaxbContext = JAXBContext.newInstance("parser.serverData.recipe");
			Marshaller marshaller = jaxbContext.createMarshaller();
			marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, new Boolean(true));
			marshaller.marshal(collection, new FileOutputStream("static_data/recipe/recipe_templates.xml"));
		}
		catch (PropertyException e)
		{
			e.printStackTrace();
		}
		catch (FileNotFoundException e)
		{
			e.printStackTrace();
		}
		catch (JAXBException e)
		{
			e.printStackTrace();
		}
	}

}
